package com.tdam2011.grupo02.appwidget.backup;

import java.util.LinkedList;

import com.tdam2011.grupo02.data.DatabaseHelperSingleton;
import com.tdam2011.grupo02.ui.MainActivity;
import com.tdam2011.grupo02.ui.MensajeWebComposicionActivity;
import com.tdam2011.grupo02.ui.MensajeriaWebFragmentActivity;
import com.tdam2011.grupo02.ui.MsgwConversationListActivity;

import com.tdam2011.grupo02.R;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;

public class ContactDroidWidgetProvider_backup extends AppWidgetProvider {

	public static int[] ids;

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		final int N = appWidgetIds.length;
		ids = appWidgetIds;

		Log.i("app widget", "actualiza");
		// Perform this loop procedure for each App Widget that belongs to this
		// provider
		for (int i = 0; i < N; i++) {
			int appWidgetId = appWidgetIds[i];

			// Get the layout for the App Widget and attach an on-click listener
			// to the button
			RemoteViews views = new RemoteViews(context.getPackageName(),
					R.layout.app_widget_container_backup);
			views.removeAllViews(R.id.contactDroidWidgetContainer);

			LinkedList<MsgwWidgetContact_backup> widgetContacts = DatabaseHelperSingleton
					.getInstance(context.getApplicationContext())
					.getDatabaseHelper().getMsgwFiveMostContacted();

			Intent intent;
			RemoteViews row = new RemoteViews(context.getPackageName(),
					R.layout.app_widget_row_backup);
			int count = 0;
			for (MsgwWidgetContact_backup widgetContact : widgetContacts) {
				if (count % 2 == 0) {
					row = new RemoteViews(context.getPackageName(),
							R.layout.app_widget_row_backup);
					views.addView(R.id.contactDroidWidgetContainer, row);
				}
				RemoteViews contact = new RemoteViews(context.getPackageName(),
						R.layout.app_widget_contact_backup);
				intent = new Intent(context, MsgwConversationListActivity.class);
				intent.putExtra("name", widgetContact.getName());
				intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
				PendingIntent pendingIntent = PendingIntent.getActivity(
						context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
				contact.setTextViewText(R.id.contactDroidWidgetContactName,
						widgetContact.getName());
				contact.setTextViewText(R.id.contactDroidWidgetContactEnv, ""
						+ widgetContact.getSent());
				contact.setTextViewText(R.id.contactDroidWidgetContactRec, ""
						+ widgetContact.getReceived());
				contact.setOnClickPendingIntent(R.id.contactDroidWidgetContact,
						pendingIntent);
				row.addView(R.id.contactDroidWidgetRow, contact);
				count++;
			}

			if (count % 2 == 0) {
				row = new RemoteViews(context.getPackageName(),
						R.layout.app_widget_row_backup);
				views.addView(R.id.contactDroidWidgetContainer, row);
			}

			RemoteViews view = new RemoteViews(context.getPackageName(),
					R.layout.app_widget_action_buttons_backup);
			row.addView(R.id.contactDroidWidgetRow, view);

			intent = new Intent(context, MensajeWebComposicionActivity.class);
			PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
					intent, 0);
			views.setOnClickPendingIntent(R.id.btnWidgetSendMsgw, pendingIntent);

			intent = new Intent(context, MensajeriaWebFragmentActivity.class);
			pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
			views.setOnClickPendingIntent(R.id.btnWidgetMsgwBandejas,
					pendingIntent);

			intent = new Intent(context, MainActivity.class);
			pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
			views.setOnClickPendingIntent(R.id.btnWidgetContactDroid,
					pendingIntent);

			// Tell the AppWidgetManager to perform an update on the current app
			// widget
			appWidgetManager.updateAppWidget(appWidgetId, views);
		}
	}
}
